// pages/member.js
import request from "../../utils/request"
Page({
  onUpdateData(e) {
    // 在这里更新父组件的数据
    console.log(e)
    this.setData({ language: e.detail.newData });
    wx.setStorage({
      key: 'language',
      data: this.data.language,
      success: function (res) {
      }
    });
  },

  /**
   * 页面的初始数据
   */
  data: {
    inputValue:[
    ],
    filteredData:[],
    isShow:'',
    unitId:'',
    language: 'zh',
    texts:{},
    is_seat_phone:'',
    background_img:'',
    event_name:'',
    event_address:'',
    event_time_address_show:'',
    event_time:'',
    top_src:'',
    searchCustom:'',
    timeAddressCustom:{},//时间地点
    query_cond:[],//查询提示
    processCustom:{},
    companyCustom:{},
    update_value:'',
    placeholdername:'',
    update_value1:'',
    placeholdervalue:'',
    event_time_address_show:'',
    query: {
      name: '',
      query:'',
    },
    buttonPosition: {
      x: 0,
      y: 0
    }
  },
  handleMove: function(e) {
    const { detail } = e;
    const { x } = detail;
    const containerWidth = 750; // movable-area的宽度
    const buttonWidth = 40; // 按钮的宽度

    // 更新按钮的位置
    this.setData({
      buttonPosition: {
        x: x
      }
    });
  },
  handleInput1:function(){
    this.setData({
      isShow : true
    })

  },
  handleInput:function(e){
    const value = e.detail.value;
    console.log(value)
    if(value.length>0){
      const filteredData = this.data.originalData.filter(item=>{
        return item.value.toString().includes(value);
      })
        this.setData({
          filteredData:filteredData
        })
        
    }else{
    this.setData({
      filteredData:[]
      })  
    }
    console.log(this.data.filteredData)
  },
  handleInput2:function(){
    this.setData({
      isShow:false
    })
  },
  handleViewTap: function(e) {
    // 获取点击的 view 的内容
 

    this.setData({

    inputValue:e.target.dataset.name,

      isShow:false,
    })
    console.log(this.data.inputValue,"10086")
    console.log(this.data.isShow)
    // 这里可以添加处理点击事件的代码
  },
  handleTouchEnd: function(e) {
    console.log(e)
    const { detail } = e;
    const { x } = detail;
    const containerWidth = 750; // movable-area的宽度
    const buttonWidth = 40; // 按钮的宽度

    // 在拖动结束时判断按钮的位置
    if (x < 50) {
      this.setData({
        buttonPosition: {
          x: 0
        }
      });
    } else if (x > containerWidth - buttonWidth - 50) {
      this.setData({
        buttonPosition: {
          x: containerWidth - buttonWidth
        }
      });
    }
  },
 


  async getPhoneNumber(e) {
    console.log(e);
    if (e.detail.errMsg === 'getPhoneNumber:ok') {
      // 用户点击允许，获取到 encryptedData 和 iv
      const { encryptedData, iv } = e.detail;
  
      // 调用 wx.login 获取 code
      wx.login({
        success: (loginRes) => {
          if (loginRes.code) {
            // 将 code 发送到后端服务器
            console.log(loginRes.code,encryptedData,iv)
            wx.request({
              url: 'https://www.femx.cn/api/query/getuserphone', // 完整的后端接口 URL
              method: 'POST',
              data: {
                code: loginRes.code,
                encryptedData: encryptedData,
                iv: iv,
                unit_id:this.data.unitId
              },
              success: async(res) => {
                // 接收后端服务器返回的手机号
                console.log(res)
                console.log('用户手机号:', res.data.phone)
                var phone = res.data.phone
                const resphone = await request({
                  url: "/checkphone",
                  data: {
                    id: this.data.index,
                    phone:phone,
                    type:2,
                  },
                  header: { 'content-type': 'application/x-www-form-urlencoded'},
                })
                console.log(resphone)
                if (resphone.code === 200) {
                  wx.navigateTo({
                    url: '/pages/query/query?param1='+JSON.stringify(resphone.data)
                  })
                }else{
                  wx.showModal({
                    title: '查询失败',
                    content: `${resphone.msg}`,
                    complete: (res) => {
                      if (res.cancel) {
                        console.log("点击取消")
                      }
                  
                      if (res.confirm) {
                        console.log("点击确认")
                      }
                    }
                  })
                }
                
              },
              fail: (error) => {
                console.error('获取手机号失败:', error);
              }
            });
          } else {
            console.log('登录失败！' + loginRes.errMsg);
          }
        }
      });
    }
  },
  onLanguageTap() {
    const that = this;
    wx.showActionSheet({
        itemList: ['English', '中文','繁体'],
        success(res) {
            if (res.tapIndex === 0) {
                that.setData({ language: 'en' });
            } else if (res.tapIndex === 1) {
                that.setData({ language: 'zh' });
            }else{
              that.setData({language:'tr'})
            }
            wx.setStorage({
              key: 'language',
              data: that.data.language,
              success: function (res) {
              }
            });
        }
    });
  },
  
  async onSubmit(e) {
    var name = e.detail.value.name;
    var phone = e.detail.value.phone;
    if (!name&&this.data.query_cond[0].checked) {
      wx.showModal({
        title: '提示', // 对话框标题
        content: '请输入姓名', // 对话框内容
        showCancel: false, // 是否显示取消按钮
        confirmText: '确定', // 确定按钮的文字，默认为确定
        confirmColor: '#3CC51F', // 确定按钮的文字颜色，默认为绿色
      })
      return false;
    }else if(!phone&&this.data.query_cond[1].checked){
      wx.showModal({
        title: '提示', // 对话框标题
        content: '请输入查询条件', // 对话框内容
        showCancel: false, // 是否显示取消按钮
        confirmText: '确定', // 确定按钮的文字，默认为确定
        confirmColor: '#3CC51F', // 确定按钮的文字颜色，默认为绿色
      })
      return false;

    }
  
    this.setData({
      'query.name': name,
      'query.query':phone
    })
    const res1 = await request({
      url: "/meetingcheck",
      data: {
        id: this.data.index,
        query: JSON.stringify(this.data.query)
      }
    })
    console.log(res1, "5555556666111")

    if (res1.code === 200) {
      wx.navigateTo({
        url: '/pages/query/query?param1='+JSON.stringify(res1.data)
      })
    } else {
      wx.showModal({
        title: this.data.texts[this.data.language].title, // 对话框标题
        content: this.data.texts[this.data.language].content, // 对话框内容
        showCancel: false, // 是否显示取消按钮
        confirmText: this.data.texts[this.data.language].confirmText, // 确定按钮的文字，默认为确定
        confirmColor: '#3CC51F', // 确定按钮的文字颜色，默认为绿色
      })
      return false;
    }
    //     wx.navigateTo({
    //    url: '/pages/query/query' 
    //  })
  },
  /**
   * 生命周期函数--监听页面加载
   */


  async onLoad(options) {
    wx.hideHomeButton();
    wx.getStorage({
      key: 'is_seat_phone', // 你要获取的数据的 key
      success:(res)=> {
       this.setData({
        is_seat_phone:res.data
       })
       console.log(this.data.is_seat_phone,"8800") // 成功回调，res.data 是你获取的数据
      },
      fail (err) {
        console.log(err) // 失败回调
      }
    })
    wx.getStorage({
      key: 'unitId', // 你要获取的数据的 key
      success:(res)=>{
        this.setData({
          unitId:res.data
        })
        console.log(res)
        console.log(this.data.unitId)
      // 成功回调，res.data 是你获取的数据
      },
      fail (err) {
        console.log(err) // 失败回调
      }
    })
    const index = JSON.parse(options.param1)
    console.log(index, "6666index")
    this.setData({
        index:index
    })
    const res1 = await request({
      url: "/banquetdetail",
      data: {
        id: index
      }
    })
    console.log(res1.data, "555555") 
    const prefix = "https://www.femx.cn"
    this.setData({
      background_img:prefix+ res1.data.content.background_img,
      event_name:res1.data.content.event_name,
      event_address:res1.data.content.event_address,
      event_time:res1.data.content.event_time,
      top_src:prefix+res1.data.content.top_src,
      event_time_address_show:res1.data.content.event_time_address_show,
      searchCustom:res1.data.content.searchCustom,
      timeAddressCustom:res1.data.content.timeAddressCustom,
      query_cond:res1.data.query_cond,
      search_bottom_text:res1.data.content.search_bottom_text,
      event_desc:res1.data.content.event_desc,
      processCustom:res1.data.content.processCustom,
      event_name:res1.data.content.event_name,
      companyCustom:res1.data.content.companyCustom,
      process_show:res1.data.content.process_show,
      event_time_address_show:res1.data.content.event_time_address_show,
      update_value:res1.data.query_cond[0].update_value,
      placeholdername:res1.data.query_cond[0].placeholder,
      update_value1:res1.data.query_cond[1].update_value,
      placeholdervalue:res1.data.query_cond[1].placeholder,
      originalData:res1.data.restaurants

    })
    const app = getApp();
    this.setData({ texts: app.globalData.lang });
    wx.getStorage({
      key: 'language',
      success:(res)=> {
       this.setData({
         language:res.data
       })
      }
    });
    console.log(this.data.texts)
  },
  onShow(){
    wx.getStorage({
      key: 'language',
      success:(res)=> {
       this.setData({
         language:res.data
       })
      }
    });
  }
})